<?php

namespace common\helpers;

use Yii;
use yii\helpers\BaseHtml;

/**
 * Class Html
 * @package common\helpers
 *
 */
class Html extends BaseHtml
{
    /**
     * 创建
     *
     * @param array $url
     * @param string $content
     * @param array $options
     * @return string
     */
    public static function create(array $url, $content = '创建', $options = [])
    {
        $options = ArrayHelper::merge([
            'class' => "btn btn-primary btn-xs"
        ], $options);

        $content = '<i class="icon ion-plus"></i> ' . $content;
        return self::a($content, $url, $options);
    }

    /**
     * 编辑
     *
     * @param array $url
     * @param string $content
     * @param array $options
     * @return string
     */
    public static function edit(array $url, $content = '编辑', $options = [])
    {
        $options = ArrayHelper::merge([
            'class' => 'btn btn-primary btn-sm',
        ], $options);

        return self::a($content, $url, $options);
    }

    /**
     * 删除
     *
     * @param array $url
     * @param string $content
     * @param array $options
     * @return string
     */
    public static function delete(array $url, $content = '删除', $options = [])
    {
        $options = ArrayHelper::merge([
            'class' => 'btn btn-danger btn-sm',
            'onclick' => "rfDelete(this);return false;"
        ], $options);

        return self::a($content, $url, $options);
    }


    /**
     * 状态标签
     *
     * @param int $status
     * @param array $options
     * @return mixed
     */
    public static function status($status = 1, $options = [])
    {
        $listBut = [
            0 => self::tag('span', '启用', array_merge(
                [
                    'class' => "btn btn-success btn-sm",
                    'onclick' => "rfStatus(this)"
                ],
                $options
            )),
            1 => self::tag('span', '禁用', array_merge(
                [
                    'class' => "btn btn-default btn-sm",
                    'onclick' => "rfStatus(this)"
                ],
                $options
            )),
        ];

        return $listBut[$status] ?? '';
    }

    /**
     * @param string $text
     * @param null $url
     * @param array $options
     * @return string
     */
    public static function a($text, $url = null, $options = [])
    {
        if ($url !== null) {
            // 权限校验
            if (!self::beforVerify($url)) {
                return '';
            }

            $options['href'] = Url::to($url);
        }

        return static::tag('a', $text, $options);
    }

    /**
     * convert html to text
     *
     * eg:   $result = spider::html2Text('<p><a href="">1</a>23<p>');
     * 输出： $result = 123
     *
     * @param $html
     * @param $sip
     * @return mixed|string
     */
    public static function html2Text($html, $sip = TRUE)
    {
        /* html_entity_decode 中 &nbsp; 会导致乱码 */
        $html = strtr($html, [
            '&nbsp;'  => ' ',
            '&rdquo;' => '”',
            '&ldquo;' => '“',
            //"\xA0" => ' ',
        ]);
        $html = preg_replace('/^[\s\t]+/is', ' ', $html);
        $html = preg_replace('#<?xml[\s\S]*?>#is', '', $html);
        $html = preg_replace('#<!--[\s\S]*?-->#is', '', $html);
        $html = preg_replace('#<!doc[\s\S]*?>#is', '', $html);
        $html = preg_replace('#<(head|script|iframe|frame|noscript|noframes|option)[\s\S]*?</\1>#is', '', $html);
        $html = preg_replace('#<(li|ol|ul|dl|h\d|dd|dt|center|form|table|tr|marquee|div|pre|blockquote|h1|h2|h3|h4|h5|h6).*?>#is', '', $html);

        if ($sip == TRUE)
        {
            $html = self::stripTags($html);
        }

        /* decode entities */
        $html = html_entity_decode($html, ENT_COMPAT, 'UTF-8');
        $html = preg_replace('#([\r\n]\s+[\r\n])+#is', "\n", $html);

        $html = str_replace(array ("\r", "\n\n"), "\n", $html);

        while (strpos($html, "\n\n") !== FALSE)
        {
            $html = str_replace("\n\n", "\n", $html);
        }

        return $html;
    }

    /**
     * alias for strip_tag / fix strip_tag unicode bug
     *
     * @param        $text
     * @param string $tags
     * @return mixed
     */
    public static function stripTags($text, $tags = '')
    {
        preg_match_all('/<([\w\-\.]+)[\s]*\/?[\s]*>/si', strtolower(trim($tags)), $tags);
        $tags = array_unique($tags[1]);
        $searches = [];

        static $block_set = [
            'head'     => 1,
            'script'   => 1,
            'iframe'   => 1,
            'frame'    => 1,
            'noscript' => 1,
            'noframes' => 1,
            'option'   => 1,
            'style'    => 1,
        ];

        /* 注释 */
        $searches[] = '#<!--[\s\S]*?-->#is';

        /* ie 判断 */
        $searches[] = '#<\!--[if[^\]]*?\]>[\S\s]<\!\[endif\]-->#is';

        if (is_array($tags) && count($tags) > 0)
        {
            $line_tags = $block_tags = '';

            foreach ($tags as $tag)
            {
                if (!$tag)
                {
                    continue;
                }

                if (isset($block_set[$tag]))
                {
                    unset($block_set[$tag]);
                }

                $line_tags .= $tag . '|';
            }

            $block_set  = array_keys($block_set);
            $block_tags = implode('|', $block_set);

            if ($block_tags)
            {
                $searches[] = '#<(' . $block_tags . ')\b[\s\S]*?</\1>#is';
            }

            if ($line_tags)
            {
                $line_tags  = substr($line_tags, 0, -1);
                $searches[] = '#<(?!(?:' . $line_tags . ')|\/(?:' . $line_tags . ')\b)[^>]*?>#si';
            }

            return preg_replace($searches, '', $text);
        }
        else
        {
            $searches[] = '#<(' . implode('|', $block_set) . ')\b[\s\S]*?</\1>#is';
            $searches[] = '#<\/?[^>]*?>#si';

            return preg_replace($searches, '', $text);
        }
    }
}